
package com.dps.omc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.enumer.EnumGroupType;
import com.dps.omc.enumer.EnumTeam;
import com.dps.omc.model.*;
import com.dps.omc.service.*;
import com.dps.omc.task.message.DesUtils;
import com.dps.omc.utils.telnet.TelnetConnect;
import com.dps.omc.utils.telnet.TelnetManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TelnetServerAction extends AbstractAdminController<ITelnetServerService>{

    @Autowired
    private ITelnetServerService telnetServerService;
    @Autowired
    private IGroupTypeService groupTypeService;
    @Autowired
    private IBizTypeService bizTypeService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentNodeService agentNodeService;
    @Autowired
    private ISystemRankService systemRankService;
    @Autowired
    private IOsTypeService osTypeService;
    @Autowired
    private TelnetManager telnetManager;
    
    @RequestMapping(value="/telnetserver/list")
    public String list(TelnetServer telnetServer, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(telnetServer); 	
    	BizData4Page<TelnetServer> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("telnetServer", telnetServer);
    	return "/module/telnetserver/list";
    }    
    
    @RequestMapping(value="/telnetserver/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<GroupType> groupTypes = groupTypeService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<Agent> agents = agentService.findAll();
        List<SystemRank> systemRanks = systemRankService.findAll();
        List<OsType> osTypes = osTypeService.findAll();

        model.addAttribute("groupTypes", groupTypes);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("systemRanks", systemRanks);
        model.addAttribute("osTypes", osTypes);
        model.addAttribute("teams", EnumTeam.values());

    	model.addAttribute("telnetServer", new TelnetServer());
        return "module/telnetserver/edit";
    }    
    
    @RequestMapping(value="/telnetserver/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<GroupType> groupTypes = groupTypeService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<Agent> agents = agentService.findAll();
        List<SystemRank> systemRanks = systemRankService.findAll();
        List<OsType> osTypes = osTypeService.findAll();

        model.addAttribute("groupTypes", groupTypes);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("systemRanks", systemRanks);
        model.addAttribute("osTypes", osTypes);
        model.addAttribute("teams", EnumTeam.values());

    	TelnetServer telnetServer = telnetServerService.fetch(id);
    	model.addAttribute("telnetServer", telnetServer);
        return "module/telnetserver/edit";
    }    
    
    @RequestMapping(value="/telnetserver/save")
    @ResponseBody
    public String save(HttpServletRequest request,TelnetServer telnetServer){
		boolean isAddNew = StringUtils.isBlank(telnetServer.getId())?true:false;
		if(isAddNew)
		{
			telnetServer.setId(IdGenerator.createNewId());
            telnetServer.setGroupType(EnumGroupType.TELNET);
			telnetServerService.add(telnetServer);
		}
		else
		{
            telnetServer.setGroupType(EnumGroupType.TELNET);
            telnetServerService.update(telnetServer);
		}
        Agent agent = agentService.fetch(telnetServer.getAgentId());

        agentNodeService.createByTelnetServer(agent,telnetServer);
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/telnetserver/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	telnetServerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(TelnetServer telnetServer)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("alias", telnetServer.getAlias());		
		conditions.put("groupType", telnetServer.getGroupType());		
		conditions.put("agentId", telnetServer.getAgentId());		
		conditions.put("host", telnetServer.getHost());		
		conditions.put("user", telnetServer.getUser());		
		conditions.put("pass", telnetServer.getPass());		
		conditions.put("port", telnetServer.getPort());		
		conditions.put("bizTypeId", telnetServer.getBizTypeId());		
		conditions.put("bizTypeName", telnetServer.getBizTypeName());		
		conditions.put("team", telnetServer.getTeam());		
		conditions.put("opSystem", telnetServer.getOpSystem());		
		conditions.put("sysRank", telnetServer.getSysRank());		
		conditions.put("note", telnetServer.getNote());		
		conditions.put("createDate", telnetServer.getCreateDate());		
    	return conditions;
    }

    //測試連接
    @RequestMapping(value="/telnetserver/testconnect")
    @ResponseBody
    public String testConnect(HttpServletRequest request) throws Exception {

        String opSystem = request.getParameter("opSystem");
        String host = request.getParameter("host");
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        pass = DesUtils.decrypt(pass);

        TelnetConnect telnetConnect = new TelnetConnect();

        telnetConnect.setOpSystem(opSystem);
        telnetConnect.setHost(host);
        telnetConnect.setUser(user);
        telnetConnect.setPass(pass);
        return telnetManager.testConnect(telnetConnect);
    }

    @Override
    protected ITelnetServerService getMainService() {
        return telnetServerService;
    }

    @Override
    protected String getMainObjName() {
        return "telnetserver";
    }

    @Override
    protected String getViewTitle() {
        return "telnetserver";
    }
}
